﻿using Microsoft.Xna.Framework;
using System;

namespace Innovator
{
	public class Component
	{
		//GameScreen that owns this component
		public GameScreen Parent;
		public bool Initialized = false;
		public bool visible = true;
		int drawOrder = 1;
		public event ComponentDrawOrderChangedEventHandler DrawOrderChanged;
		public Component(GameScreen Parent)
		{
			InitializeComponent(Parent);
		}
		public Component()
		{
			InitializeComponent(Engine.DefaultScreen);
		}
		protected virtual void InitializeComponent(GameScreen Parent)
		{
			if (!Engine.IsInitialized)
				throw new System.Exception("Engine must be initialized with \"SetupEngine()\"" + "before components can be initialized");
			Parent.Components.Add(this);
			Initialized = true;
		}
		public virtual void Update()
		{
		}
		public virtual void Draw()
		{
		}
		public virtual void DisableComponent()
		{
			Parent.Components.Remove(this);
		}
		public int DrawOrder
		{
			get { return drawOrder; }
			set
			{
				int last = drawOrder;
				drawOrder = value;
				if (DrawOrderChanged != null)
					DrawOrderChanged(this,
						new ComponentDrawOrderChangedEventArgs(
						this, last, this.Parent.Components));
			}
		}
	}
	public class ComponentDrawOrderChangedEventArgs : EventArgs
	{
		public Component Component;
		public int LastDrawOrder;
		public ComponentCollection ParentCollection;
		public ComponentDrawOrderChangedEventArgs(Component Component,
			int LastDrawOrder, ComponentCollection ParentCollection)
		{
			this.Component = Component;
			this.LastDrawOrder = LastDrawOrder;
			this.ParentCollection = ParentCollection;
		}
	}
	public delegate void ComponentDrawOrderChangedEventHandler(
		object sender, ComponentDrawOrderChangedEventArgs e);
}
